#!/bin/bash -x

# ========================================================================
# reset + cleanup
do_cleanup () {
  echo "Do cleanup"
  command -v kubectl > /dev/null
  if [ $? -eq 0 ]; then
  kubectl delete -k $(pwd)/trustee/kbs/config/kubernetes/overlays || true
  kubectl delete -k $(pwd)/operator/config/samples/ccruntime/default/ || true
  kubectl delete -k $(pwd)/operator/config/release/ || true
  fi

  command -v kubeadm > /dev/null
  if [ $? -eq 0 ]; then
  sudo kubeadm reset
  fi

  command -v ctr > /dev/null
  if [ $? -eq 0 ]; then
  sudo ctr -n k8s.io image ls | awk '{print $1}' | xargs -n1 sudo ctr -n k8s.io image rm
  fi

  sudo systemctl stop docker || true
  sudo systemctl stop containerd || true
  sudo pkill -f containerd || true
  # cleanup metadatas, if not, the nydus may work improperly
  sudo rm -rf /tmp/nydus-snapshotter || true # just for local
  sudo rm -rf /opt/confidential-containers || true
  sudo rm -rf /opt/containerd/ || true
  sudo rm -rf /var/run/containerd-nydus || true
  sudo rm -rf /var/run/containerd || true
  sudo rm -rf /var/lib/containerd-nydus || true
  sudo rm -rf /var/lib/containerd || true
  sync
}

do_cleanup

# ========================================================================
setup_system () {
  echo "Setup system"
  # inhibit swap
  # 临时禁用
  sudo swapoff -a
  # 永久禁用，编辑 /etc/fstab 文件，注释掉包含 swap 的行
  sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab

  # ========================================================================
  # kernel params
  # 加载必要的内核模块
  sudo modprobe overlay
  sudo modprobe br_netfilter

  # 创建并编辑系统参数文件
  cat <<EOF | sudo tee /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

  # 应用系统参数
  sudo sysctl --system
}

setup_system

# ========================================================================
install_docker_containerd () {
  echo "Install docker & containerd"
  # install docker+containerd
  # 添加 GPG 密钥
  curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | \
    sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

  # 添加 Docker 软件源
  echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | \
    sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

  sudo apt update

  #sudo apt remove -y docker-ce docker-ce-cli containerd.io
  sudo apt install -y docker-ce docker-ce-cli containerd.io

  if [ -e /etc/docker/daemon.json ] && [ $(wc -l /etc/docker/daemon.json | awk '{print $1}') -gt 2 ]; then
    if [ $(grep -c 'registry.cn-hangzhou.aliyuncs.com' /etc/docker/daemon.json) -eq 0 ]; then
      sed -i "1a\  \"registry-mirrors\": [\"https://registry.cn-hangzhou.aliyuncs.com\"]," \
        /etc/docker/daemon.json
    fi
  else
    sudo mkdir -p /etc/docker || true
    cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "registry-mirrors": ["https://registry.cn-hangzhou.aliyuncs.com"]
}
EOF
  fi

  sudo containerd config default | sudo tee /etc/containerd/config.toml
  sudo sed -i 's!\(sandbox_image = \).*pause:.*$!\1"registry.aliyuncs.com/google_containers/pause:3.8"!' \
    /etc/containerd/config.toml
  #sudo sed -i 's!\(systemd_cgroup = \).*$!\1true!' /etc/containerd/config.toml
  sudo systemctl daemon-reload
  sudo systemctl restart containerd

  # 重启 Docker 服务
  sudo systemctl restart docker
  sudo systemctl enable docker
}

install_docker_containerd

# ========================================================================
install_k8s () {
  echo "Install k8s"
  # install kubeadm+kubelet+kubectl
  # 添加 GPG 密钥
  curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -

  # 添加 Kubernetes 软件源
  cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF

  sudo apt update

  sudo apt install -y --allow-downgrades \
    --allow-change-held-packages kubelet=1.28.0-00 kubeadm=1.28.0-00 kubectl=1.28.0-00
  sudo apt-mark hold kubelet kubeadm kubectl
}

install_k8s

# ========================================================================
load_k8s_images () {
  echo "Loading k8s images"
  # load k8s images from local FS can speedup deploy
  if [ ! -d k8s.init.images ]; then
    exit 1
  fi
  for f in $(ls k8s.init.images/*.tar); do
    sudo ctr -n k8s.io image import $f
  done
}

#load_k8s_images

init_k8s () {
  echo "Initiate k8s"
  sudo -E kubeadm init --kubernetes-version=v1.28.0 \
    --image-repository registry.aliyuncs.com/google_containers \
    --pod-network-cidr=10.244.0.0/16

  rm $HOME/.kube -rf || true
  mkdir $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

  # apply net cni
  kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

  # setup roles
  kubectl get nodes --show-labels
  k8s_node_name=$(kubectl get nodes | grep control-plane | awk '{print $1}')
  kubectl taint nodes $k8s_node_name node-role.kubernetes.io/control-plane:NoSchedule-
  kubectl label nodes $k8s_node_name node-role.kubernetes.io/worker=
  kubectl label nodes $k8s_node_name node.kubernetes.io/worker=
  kubectl get nodes --show-labels
}

init_k8s

install_k8s_tools () {
  echo "Install k8s tools"
  sudo snap install kustomize
}

install_k8s_tools
